package com.example.xd.android_vresion_test.android_7;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.role.RoleManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.BlockedNumberContract;
import android.telecom.CallScreeningService;
import android.view.View;
import android.widget.Toast;

import com.example.xd.android_vresion_test.R;

import static android.content.Context.*;

public class Android7Activity extends AppCompatActivity implements View.OnClickListener {

    private static final int REQUEST_ID = 1;
    private ContentValues mContentValues;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android7);
//        for (int i = 0; i < 3; i++) {
//            mContentValues = new ContentValues();
//            mContentValues.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1570000" + i);
//            getContentResolver().insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, mContentValues);
//        }
        findViewById(R.id.v7_btn1).setOnClickListener(this);


    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v7_btn1:
                test1();
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void test1() {
        RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
        Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
        startActivityForResult(intent, REQUEST_ID);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID) {
            if (resultCode == android.app.Activity.RESULT_OK) {
                startService(new Intent(Android7Activity.this, MyService.class));
            } else {
                Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
